#include "Follows.h"

Follows::Follows()
{
}

Follows::~Follows()
{
}

void Follows::InsertFollows(const STMT statement_1, const STMT statement_2)
{
	/*
	 Should also check for conflicting... 
	 i.e. (1,2) exist yet inserting (1,3)
	 */

	if ((statement_2.statement_number > statement_1.statement_number) &&
		(statement_1.statement_number >= 1) &&
		(statement_2.statement_number >= 2))
	{
		this->s1_table.insert(std::pair<STMT, STMT>(statement_1,statement_2));
		this->s2_table.insert(std::pair<STMT, STMT>(statement_2,statement_1));
	}
}

STMT Follows::GetPrevious(const STMT following_statement)
{
	if (following_statement.statement_number >= 2)
	{
		std::map<STMT, STMT>::const_iterator it = s2_table.find(following_statement);
		if (it != s2_table.end())
		{
			return it->second;
		}
	}

	STMT s;
	s.node_type = _NULL;
	s.statement_number = -1;

	return s;
}

STMT Follows::GetFollowing(const STMT previous_statement)
{
	if (previous_statement.statement_number >= 1)
	{
		std::map<STMT, STMT>::const_iterator it = s1_table.find(previous_statement);
		if (it != s1_table.end())
		{
			return it->second;
		}
	}
	
	STMT s;
	s.node_type = _NULL;
	s.statement_number = -1;

	return s;
}

STMT_ARRAY Follows::RetrieveTransitivePrevious(const STMT following_statement)
{
	STMT_ARRAY stmt_array;

	if (following_statement.statement_number >= 2)
	{
		STMT prev = this->GetPrevious(following_statement);
		while (prev.statement_number > 0)
		{
			stmt_array.insert(prev);
			prev = this->GetPrevious(prev);
		}
	}

	return stmt_array;
}

STMT_ARRAY Follows::RetrieveTransitiveFollowing(const STMT previous_statement)
{
	STMT_ARRAY stmt_array;

	if (previous_statement.statement_number >= 1)
	{
		STMT follow = this->GetFollowing(previous_statement);
		while (follow.statement_number > 1)
		{
			stmt_array.insert(follow);
			follow = this->GetFollowing(follow);
		}

		/*
		std::map<STMT, STMT>::const_iterator it = s1_table.find(s2);

		while (it != s1_table.end())
		{
			s2 = it->second;
			stmt_array.insert(s2);
			it = s1_table.find(s2);
		} */
	}

	return stmt_array;
}